#import "CustomTableCell.h"

@implementation CustomTableCell
- (id) init 
{
	//NSLog(@"init CustomTableCell");
	
	//the super is the _table where the cels reside
	self = [super init];
	if( ! self ) return nil;
	
// not needed for this table view as use other movement ways
//	[self setShowDisclosure:YES];
//	[self setDisclosureClickable:YES];
//	[self setDisclosureStyle:0];

	_counterNameLabel = [[UITextLabel alloc] init];
	_secondsLabel = [[UITextLabel alloc] init];
	_infoCounterLabel = [[UITextLabel alloc] init];
	_counterIconImageView = [[UIImageView alloc] init];
	_topLabel = [[UITextLabel alloc] init];
	_dtype = [[NSNumber alloc]initWithInt: 1];
	
	[self addSubview:_counterNameLabel];
	[self addSubview:_secondsLabel];
	[self addSubview:_infoCounterLabel];
	[self addSubview:_counterIconImageView];
	[self addSubview:_topLabel];

// this can be used to enable tap operations on the table cell icon
//	[_counterIconImageView setTapDelegate:self];

	[_counterIconImageView setImage:[UIImage applicationImageNamed:@"seconds.png"]];

	CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
	
	float grayComponents[4] = { 0.3, 0.3, 0.3, 1. };
	float whiteComponents[4] = { 1., 1., 1., 1. };
	float transparentWhiteComponents[4] = { 1., 1., 1., 0. };
	float blackComponents[4] = {0.,0.,0.,1.};
		
	[_counterNameLabel setFont:[NSClassFromString(@"WebFontCache") createFontWithFamily:@"Helvetica" traits:0 size:17.]];
	[_counterNameLabel setColor:CGColorCreate(colorSpace, grayComponents)];
	[_counterNameLabel setHighlightedColor:CGColorCreate(colorSpace, whiteComponents)];
	[_counterNameLabel setBackgroundColor:CGColorCreate(colorSpace, transparentWhiteComponents)];

	float blueComponents[4] = { 0.19607843, 0.29803922, 0.84313725, 1. };

	[_secondsLabel setFont:[NSClassFromString(@"WebFontCache") createFontWithFamily:@"Helvetica" traits:2 size:42.]];
	[_secondsLabel setColor:CGColorCreate(colorSpace, blackComponents)];
	[_secondsLabel setHighlightedColor:CGColorCreate(colorSpace, whiteComponents)];
	[_secondsLabel setBackgroundColor:CGColorCreate(colorSpace, transparentWhiteComponents)];

	[_infoCounterLabel setFont:[NSClassFromString(@"WebFontCache") createFontWithFamily:@"Helvetica" traits:0 size:12.]];
	[_infoCounterLabel setColor:CGColorCreate(colorSpace, grayComponents)];
	[_infoCounterLabel setHighlightedColor:CGColorCreate(colorSpace, whiteComponents)];
	[_infoCounterLabel setBackgroundColor:CGColorCreate(colorSpace, transparentWhiteComponents)];
	
	[_topLabel setFont:[NSClassFromString(@"WebFontCache") createFontWithFamily:@"Helvetica" traits:2 size:25.]];
	[_topLabel setColor:CGColorCreate(colorSpace, blackComponents)];
	[_topLabel setHighlightedColor:CGColorCreate(colorSpace, whiteComponents)];
	[_topLabel setBackgroundColor:CGColorCreate(colorSpace, transparentWhiteComponents)];
	
	return self;
}



// * custom table cell display
- (void) layoutSubviews 
{
	//NSLog(@"layoutSubviews");
	[super layoutSubviews];

	CGRect contentBounds = [self contentBounds]; // using contentBounds instead of frame because it gets updated for remove and reording controls being visible
	CGRect frame = contentBounds;

	//NSLog(@"init Counter Name label");
	frame.origin.y = contentBounds.origin.y + 1.;
	frame.origin.x = contentBounds.origin.x + 2.;
	frame.size.height = 20.;
	frame.size.width = contentBounds.size.width - 2.;
	[_counterNameLabel setFrame:frame];

	//NSLog(@"init Type Image");
	frame.origin.y = contentBounds.origin.y + 69.;
	frame.origin.x = contentBounds.origin.x + 1.;
	frame.size.width = 30.;
	frame.size.height = 30.;
	[_counterIconImageView setFrame:frame];

	switch([_dtype intValue])
	{
	case 0://Days
		frame.origin.y = contentBounds.origin.y + 23.;
		frame.origin.x = contentBounds.origin.x + 90.;
		frame.size.height = 50.;
		frame.size.width = contentBounds.size.width - 90.;
		[_secondsLabel setFrame:frame];
		
		frame.origin.y = contentBounds.origin.y + 35.;
		frame.origin.x = contentBounds.origin.x + 2.;
		frame.size.height = 30.;
		frame.size.width = 88.;
		[_topLabel setFrame:frame];		
		break;
	case 1://seconds
		frame.origin.y = contentBounds.origin.y + 23.;
		frame.origin.x = contentBounds.origin.x + 2.;
		frame.size.height = 45.;
		frame.size.width = contentBounds.size.width - 2.;
		[_secondsLabel setFrame:frame];
		break;
	case 2://Moons
		frame.origin.y = contentBounds.origin.y + 23.;
		frame.origin.x = contentBounds.origin.x + 78.;
		frame.size.height = 50.;
		frame.size.width = contentBounds.size.width - 78.;
		[_secondsLabel setFrame:frame];
		
		frame.origin.y = contentBounds.origin.y + 35.;
		frame.origin.x = contentBounds.origin.x + 2.;
		frame.size.height = 30.;
		frame.size.width = 76.;
		[_topLabel setFrame:frame];
		break;
	}
		
	//NSLog(@"init Counter info label");
	frame.origin.y = contentBounds.origin.y + 75.;
	frame.origin.x = contentBounds.origin.x + 33.;
	frame.size.height = 14.;
	frame.size.width = contentBounds.size.width - 33.;
	[_infoCounterLabel setFrame:frame];

}


- (void) updateHighlightColors 
{
	//NSLog(@"updateHighlightColors");
	[super updateHighlightColors];

	[_counterNameLabel setHighlighted:![_counterNameLabel isHighlighted]];
	[_secondsLabel setHighlighted:![_secondsLabel isHighlighted]];
	[_topLabel setHighlighted: ![_topLabel isHighlighted]];
	[_infoCounterLabel setHighlighted: ![_infoCounterLabel isHighlighted]];
}


- (UITextLabel *) counterNameTextLabel 
{
	return _counterNameLabel;
}

- (void) setCounterName:(NSString *) text 
{
	[_counterNameLabel setText:text];
}

- (NSString *) counterName 
{
	return [_counterNameLabel text];
}

- (void) setSeconds:(NSString *) text withSpecial: (NSString *) topText
{
	[_secondsLabel setText:text];
	[_topLabel setText:topText];
	[self layoutSubviews];
}

- (NSString *) counterSeconds 
{
	return [_secondsLabel text];
}

- (void) setDisplayType:(NSNumber *) dtype 
{
	switch([dtype intValue])
	{
	case 0://Days
		[_infoCounterLabel setText:@"Days           Hours         Minutes         Seconds"];
		[self addSubview:_topLabel];
		[_secondsLabel setFont:[NSClassFromString(@"WebFontCache") createFontWithFamily:@"Helvetica" traits:2 size:45.]];
		[_counterIconImageView setImage:[UIImage applicationImageNamed:@"sun.png"]];
		break;
	case 1:
		[_infoCounterLabel setText:@"      Seconds"];
		[_topLabel removeFromSuperview];
		[_secondsLabel setFont:[NSClassFromString(@"WebFontCache") createFontWithFamily:@"Helvetica" traits:2 size:42.]];
		[_counterIconImageView setImage:[UIImage applicationImageNamed:@"seconds.png"]];
		break;
	case 2:  
		[_infoCounterLabel setText:@"Moons     Days      Hours      Minutes       Seconds"];
		[self addSubview:_topLabel];
		[_secondsLabel setFont:[NSClassFromString(@"WebFontCache") createFontWithFamily:@"Helvetica" traits:2 size:38.]];
		[_counterIconImageView setImage:[UIImage applicationImageNamed:@"moon.png"]];
		break;
	}
	_dtype = dtype;
}

// ------------------------Delegate Functions --------------------------

- (NSString*) description
{
	NSString *desc = [[NSString alloc] initWithFormat:@"Name: %@ >> %@",[self counterName], [self counterSeconds]];
	return desc;
}

@end
